package org.raymond.iworks.apis.common;

import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    //成功标志
    private boolean success = false;

    //返回处理消息
    private String message = "操作成功！";

    //返回处理详情
    private String detailMessage;

    private Integer code = 0;

    private T data;

    private String uri;

    private String method;

    private long timestamp = System.currentTimeMillis();

    private Result() {
    }

    public static<T> Result<T> success(){
        return success("成功", null);
    }

    public static<T> Result<T> success(T data) {
        return success("成功", data);
    }

    public static<T> Result<T> success(String msg, T data) {
        Result<T> r = new Result<>();
        r.setMessage(msg);
        r.setData(data);
        r.setCode(ErrorCode.OK.getCode());
        r.setSuccess(true);
        return r;
    }

    public static<T> Result<T> error(String msg) {
        return error(ErrorCode.ERROR.getCode(), msg);
    }

    public static<T> Result<T> error(int code, String msg) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public static<T> Result<T> error(ErrorCode errorCode) {
        Result<T> r = new Result<>();
        r.setCode(errorCode.getCode());
        r.setMessage(errorCode.getMessage());
        r.setSuccess(false);
        return r;
    }

    public static Result<Boolean> result(String s, String f, Boolean result){
        if(result){
            return success(s, result);
        }else{
            return error(f);
        }
    }

    public Result<T> detail(String detailMessage){
        this.detailMessage = detailMessage;
        return this;
    }

    public Result<T> uri(String uri){
        this.uri = uri;
        return this;
    }

    public Result<T> method(String method){
        this.method = method;
        return this;
    }
}
